package com.karlis.controller;

import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.apache.log4j.Logger;
import com.karlis.domain.Warehouse;
import com.karlis.dto.WarehouseDTO;
import com.karlis.service.ItemService;
import com.karlis.service.WarehouseService;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/record")
public class MainController {
	
	protected static Logger logger = Logger.getLogger("controller");
	
	@Resource(name="warehouseService")
	 private WarehouseService warehouseService;
	  
	 @Resource(name="itemService")
	 private ItemService itemService;
	 
	 /**
	  * Retrieves the "Records" page
	  */
	 @Secured("ROLE_USER")
	    @RequestMapping(value = "/list", method = RequestMethod.GET)
	    public String getRecords(Model model) {
	     logger.debug("Received request to show records page");
	      
	     // Retrieve all warehouses
	     List<Warehouse> warehouses = warehouseService.getAll();
	      
	     // Prepare model object
	     List<WarehouseDTO> warehousesDTO = new ArrayList<WarehouseDTO>();
	      
	     for (Warehouse warehouse: warehouses) {
	      // Create new data transfer object
	    	 WarehouseDTO dto = new WarehouseDTO();
	       
	   dto.setId(warehouse.getId());
	   dto.setName(warehouse.getName());
	   dto.setAddress(warehouse.getAddress());
	   dto.setManager(warehouse.getManager());
	   dto.setItems(itemService.getAll(warehouse.getId()));
	    
	   // Add to model list
	   warehousesDTO.add(dto);
	     }
	      
	     // Add to model
	     model.addAttribute("warehouses", warehousesDTO);
	 
	     // This will resolve to /WEB-INF/jsp/records.jsp
	  return "records";
	 }

	     
	    /**
	     *  Retrieves the "Add New Record" page
	     */
	 @Secured("ROLE_USER")
	    @RequestMapping(value = "/add", method = RequestMethod.GET)
	    public String getAdd(Model model) {
	     logger.debug("Received request to show add page");
	     
	     // Create new Person and add to model
	     model.addAttribute("warehouseAttribute", new Warehouse());
	 
	     // This will resolve to /WEB-INF/jsp/add-record.jsp
	     return "add-record";
	 }
	    
	    /**
	     * Adds a new record
	     */
	    @RequestMapping(value = "/add", method = RequestMethod.POST)
	    public String postAdd(@ModelAttribute("warehouseAttribute") Warehouse warehouse) {
	  logger.debug("Received request to add new record");
	 
	  // Delegate to service
	  warehouseService.add(warehouse);
	 
	  // Redirect to url
	  return "redirect:/karlis/record/list";
	 }
	 
	    /**
	     * Deletes a record including all the associated credit cards
	     */
	    @Secured("ROLE_USER")
	    @RequestMapping(value = "/delete", method = RequestMethod.GET)
	    public String getDelete(@RequestParam("id") Integer warehouseId) {
	     logger.debug("Received request to delete record");
	      
	     // Delete person
	  warehouseService.delete(warehouseId);
	 
	  // Redirect to url
	  return "redirect:/karlis/record/list";
	 }
	    
	    /**
	     * Retrieves the "Edit Existing Record" page
	     */
	    @Secured("ROLE_USER")
	    @RequestMapping(value = "/edit", method = RequestMethod.GET)
	    public String getEdit(@RequestParam("id") Integer warehouseId, Model model) {
	     logger.debug("Received request to show edit page");
	      
	     // Retrieve person by id
	     Warehouse existingWarehouse = warehouseService.get(warehouseId);
	 
	     // Add to model
	     model.addAttribute("warehouseAttribute", existingWarehouse);
	 
	     // This will resolve to /WEB-INF/jsp/edit-record.jsp
	     return "edit-record";
	 }
	  
	    /**
	     * Edits an existing record
	     */
	    @Secured("ROLE_USER")
	    @RequestMapping(value = "/edit", method = RequestMethod.POST)
	    public String postEdit(@RequestParam("id") Integer warehouseId, 
	              @ModelAttribute("warehouseAttribute") Warehouse warehouse) {
	  logger.debug("Received request to edit existing person");
	   
	  // Assign id
	  warehouse.setId(warehouseId);
	   
	  // Delegate to service
	  warehouseService.edit(warehouse);
	 
	  // Redirect to url
	  return "redirect:/karlis/record/list";
	 }

}
